/*
 * Charles Douglas
 * CSCE 4230 Program 1
 * Feb 3, 2009
 *
 * */

#include <math.h>
#include <GL/glut.h>
#define min(a,b) ((a) > (b) ? (b) : (a))

double midpoint_x=0.5;
double midpoint_y=0.5;

double start_x=0.5;
double start_y=0.5;

double max_radius=0.45;

/*
 * these are assigned values in init() -- they form an equilateral triangle
 */
double anchor1_x;
double anchor1_y;

double anchor2_x;
double anchor2_y;

double anchor3_x;
double anchor3_y;



/* converts degrees to radians */
double d2r(double degrees)
{
    return (degrees * M_PI) / 180.0;
}

/* gets the midpoint of 2 numbers -- to get the midpoint of 2 points, just repeat for x and y coordinates*/
double midpoint(double p1, double p2)
{
    double diff = fabs(p1 - p2) / 2.0;
    return min(p1, p2) + diff; 
}

/* returns a random int 1-3 inclusive */
int random1to3(void)
{
    return (rand() % 3) + 1;
}

void display(void)
{
    double current_x;
    double current_y;
    int anchor;
    int i;

    glClearColor (0.0, 0.0, 0.0, 0.0);
    glClear (GL_COLOR_BUFFER_BIT);
    glColor3f (1.0, 1.0, 1.0);

    current_x = start_x;
    current_y = start_y;
    for(i=1; i<=30000; ++i)
    {
        /*draw current point*/
        glBegin(GL_POINTS);
            glVertex2d(current_x, current_y);
        glEnd();
        
        /*pick an anchor*/
        anchor=random1to3();
        switch(anchor)
        {
            case 1:
                current_x = midpoint(current_x, anchor1_x);
                current_y = midpoint(current_y, anchor1_y);
            break;
            
            case 2:
                current_x = midpoint(current_x, anchor2_x);
                current_y = midpoint(current_y, anchor2_y);
            break;

            case 3:
                current_x = midpoint(current_x, anchor3_x);
                current_y = midpoint(current_y, anchor3_y);
            break;
        }        
    }
    glFlush ();
}


void reshape (int w, int h)
{
    glViewport (0, 0, 
        min((GLsizei) w, (GLsizei) h), min((GLsizei) w, (GLsizei) h));
    gluOrtho2D (0.0,(GLdouble) 1.0, 0.0,(GLdouble) 1.0);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();

}

void keypress(unsigned char key, int x, int y) 
{
    if (key == 27) exit(0);
}


void init (void) 
{

    
    gluOrtho2D (0.0,(GLdouble) 1.0, 0.0,(GLdouble) 1.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    
    /* the anchor points will form an equilateral triangle 
        centered on the midpoint and will fit in the circle 
        with radius max_radius */

    anchor1_x = midpoint_x;
    anchor1_y = midpoint_x + max_radius;

    anchor2_x = midpoint_x - max_radius * cos(d2r(30));
    anchor2_y = midpoint_y - max_radius * sin(d2r(30));

    anchor3_x = midpoint_x + max_radius * cos(d2r(30));
    anchor3_y = midpoint_y - max_radius * sin(d2r(30));
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize (500, 500); 
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("Homework 1");
    
    init ();

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutKeyboardFunc(keypress);

    glutMainLoop();
    return 0;
}

